package org.ilda.func.utility;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.ilda.bean.IURL;


public class UrlUtility {
	
	public static List<IURL> urlList = new ArrayList<IURL>();
	
	/**
	 * 获取网页标题<title></title>
	 * @param content
	 * @return
	 */
	public static String getTitleName(String content){
		return StringUtility.getSubString(content, "<title>", "</title>");
	}
	
	
	public static void extractURL(String baseUrl, String content){
		
		Pattern pattern = Pattern.compile("<a href=.*</a>");
		Matcher matcher = pattern.matcher(content);
		
		String urls = null;
		
		while(matcher.find()){
			urls = matcher.group();
			pushUrl(baseUrl, urls);
		}
	}
	
	
	
	
	public static void pushUrl(String baseUrl, String urls){
		List<String> list = StringUtility.getSubStrArray(urls, "<a href=\"", "\"");
		
		for(String str : list){
			String tmpurl = formatUrl(baseUrl, str);
			if(tmpurl == null) continue;
			
			IURL url = new IURL(tmpurl, "Non_Process");
			
			if(!urlList.contains(url)){
				urlList.add(url);
			}
		}
		
	}
	
	
	public static void main(String[] args) {
		
		String baseUrl = "http://bbs.tianya.cn/";
		String link = "javascript:void(0);";
		
		IURL url = new IURL(formatUrl(baseUrl, link), "Non_Process");
		
		if(!urlList.contains(url)){
			urlList.add(url);
		}

		
	}
	
	
	/**
	 * 格式化URL
	 * @param baseUrl
	 * @param link
	 * @return
	 */
	private static String formatUrl(String baseUrl, String link){
		if(baseUrl == null || link == null){
			return null;
		}
		
		Url base = Parse(baseUrl); 
		Url url = Parse(link);
		
		if(base == null || url == null){
			return null;
		}
		
		System.out.println("-----------------");
		System.out.println(baseUrl);
		System.out.println(link);
		System.out.println(url.schema);
		System.out.println("-----------------");
		
		if(url.schema == null){
			url.schema = base.schema;
		}
		if(url.host == null){
			url.host = base.host;
		}
				
		return url.ToUrl();
	}
	
	
	
	private static Url Parse(String link){
		
		//System.out.println("--link --- is --"+link);
		
		int idx, endIdx;
		
		Url url = new Url();
		
		if((idx = link.indexOf("#")) >= 0 ){
			if(idx == 0) return null;
			link = link.substring(0, idx);
		}
		
		if((idx = link.indexOf(":")) >= 0){
			url.schema = link.substring(0, idx);
			if(isLegalSchema(url.schema)){
				link = link.substring(idx + 1);
			}else{
				return null;
			}
			
		}
		if(link.startsWith("//")){
			if((endIdx = link.indexOf('/', 2)) > 0){
				url.host = link.substring(2, endIdx);
				link = link.substring(endIdx + 1);
			}else{
				link = link.substring(2);
			}
		}
		if(link != null){
			url.path = link.trim();
		}else{
			url.path = "";
		}
			
		
		return url;
	}
	
	private static boolean isLegalSchema(String schema){
		if(schema.equals("http") || schema.equals("https") || schema.equals("ftp")) return true;
		else return false;
	}
	
	
	private static class Url{
		
		public Url(){};
		
		public String ToUrl(){
			String prefix = null;
			if(path.startsWith("/")){
				prefix = schema + "://" + host;
			}
			else{
				prefix = schema + "://" + host + "/";
			}
			return prefix + path;
		}
		
		public String schema;
		public String host;
		public String path;
	}
	
	
	
	

}

